package org.itboys.store.weixin;

import org.apache.ibatis.annotations.Param;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.fuzion.entity.FuzionMember;
import org.itboys.store.dto.ConfirmOrderDto;
import org.itboys.store.entity.ReceiveAddress;
import org.itboys.store.service.StoreReceiveAddressService;
import org.itboys.store.service.StoreReceiveAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by huml on 2016/4/19.
 * 收货地址控制层
 */
@RestController
@RequestMapping("/store/weixin/address")
public class StoreAddressWeixinController extends BaseController {

    @Autowired
    private StoreReceiveAddressService receiveAddressService;

    /**
     * 我的地址列表
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/addressList")
    public ModelAndView addressList(HttpServletRequest request,
                                    @RequestParam(value = "productId", required = false) Long productId,
                                    @RequestParam(value = "carIds", required = false)List<Long> carIds,
                                    @RequestParam(value = "num", required = false)Integer num,
                                    Model model){
        FuzionMember member = (FuzionMember) request.getSession().getAttribute("member");
        List<ReceiveAddress> list = receiveAddressService.getAddressesByMemberId(member.getId());
        model.addAttribute("addresses", list);
        model.addAttribute("carIds", carIds.toString().replace("[", "").replace("]", ""));
        model.addAttribute("productId", productId);
        model.addAttribute("num", num);
        return new ModelAndView("site/address");
    }

    /**
     * 跳往新增地址页面
     * @param model
     * @return
     */
    @RequestMapping("/addAddressVM")
    public ModelAndView addAddressVM(@RequestParam(value = "productId", required = false) Long productId,
                                     @RequestParam(value = "carIds", required = false)List<Long> carIds,
                                     @RequestParam(value = "num", required = false)Integer num,
                                     Model model){
        model.addAttribute("carIds", carIds.toString().replace("[", "").replace("]", ""));
        model.addAttribute("productId", productId);
        model.addAttribute("num", num);
        return new ModelAndView("site/addAddress");
    }

    /**
     * 添加地址
     * @param addressId
     * @param provinceId
     * @param cityId
     * @param districtId
     * @param address
     * @param identityNo
     * @param name
     * @param mobile
     * @param request
     * @param response
     */
    @RequestMapping("/addAddress")
    public void addAddress(@RequestParam("addressId")Long addressId, @RequestParam("provinceId")Long provinceId,
                           @RequestParam("cityId")Long cityId, @RequestParam("districtId")Long districtId,
                           @RequestParam("address")String address,
                           @RequestParam("identityNo")String identityNo, @RequestParam("name")String name,
                           @RequestParam("mobile")String mobile, HttpServletRequest request,
                           HttpServletResponse response){
        try {
            FuzionMember member = (FuzionMember) request.getSession().getAttribute("member");
            receiveAddressService.save(addressId, provinceId, cityId, districtId, address, identityNo, name, mobile, member.getId());
            AjaxUtils.renderJsonporJson(response, null, ResultPageDOHelper.getErrorResultDO(0, CommonConstants.SUCCESS));
        } catch (Exception e) {
            logger.error("/store/weixin/address/addAddress error", e);
            AjaxUtils.renderJsonporJson(response, null, ResultPageDOHelper.getErrorResultDO(-1, CommonConstants.FAIL));
        }
    }

    /**
     * 设置默认地址
     * @param request
     * @param addressId
     * @param callback
     * @param response
     */
    @RequestMapping("/setDefaultAddress")
    public void setDefaultAddress(HttpServletRequest request,
                                  @RequestParam("addressId")Long addressId,
                                  @Param("callback")String callback,
                                  HttpServletResponse response){
        try {
            FuzionMember member = (FuzionMember) request.getSession().getAttribute("member");
            receiveAddressService.setDefaultAddress(member.getId(), addressId);
            AjaxUtils.renderJsonporJson(response, callback, ResultPageDOHelper.getErrorResultDO(0, CommonConstants.SUCCESS));
        } catch (Exception e){
            logger.error("/weixin/address/RequestMapping error", e);
            AjaxUtils.renderJsonporJson(response, callback, ResultPageDOHelper.getErrorResultDO(-1, CommonConstants.FAIL));
        }
    }

    /**
     * 删除地址
     * @param addressId
     * @param callback
     * @param response
     */
    @RequestMapping("/deleteAddress")
    public void deleteAddress(@RequestParam("addressId")Long addressId,
                              @Param("callback")String callback,
                              HttpServletResponse response){
        try {
            receiveAddressService.deleteAddress(addressId);
            AjaxUtils.renderJsonporJson(response, callback, ResultPageDOHelper.getErrorResultDO(0, CommonConstants.SUCCESS));
        } catch (Exception e){
            logger.error("/weixin/address/deleteAddress error", e);
            AjaxUtils.renderJsonporJson(response, callback, ResultPageDOHelper.getErrorResultDO(-1, CommonConstants.FAIL));
        }
    }


}
